/**
 * This class is responsible for tracking the players inventory, consisting of
 * water, food, repair and money items.
 * 
 * @author Jon Thomas
 */
public class ItemBag
{
    private int food, water, repair, medicine, money, weight; // item amounts
    public int foodW, waterW, repairW, medicineW; // item weight values
    private final int maxWeight = 250;

    /**
     * Water and food initialized to 20 and repair to 5 and money to 100.
     */
    public ItemBag()
    {
        water = 0;
        food = 0;
        repair = 0;
        medicine = 0;
        money = 250;

        weight = 0;
        foodW = 2;
        waterW = 3;
        repairW = 15;
        medicineW = 1;

        updateWeight();
    }
    


    /**
     * refreshes the weight of the items onboard the ship
     */
    private void updateWeight()
    {
        weight = food * foodW + water * waterW + repair * repairW + medicine
                * medicineW;
    }

    /**
     * Checks if weight limits are surpassed
     * @param itemName name of the item
     * @return boolean true or false whether 
     */
    public boolean enoughWeight(String itemName)
    {
        if (itemName == "food") if (weight + foodW <= maxWeight) return true;
        if (itemName == "water") if (weight + waterW <= maxWeight) return true;
        if (itemName == "repair")
            if (weight + repairW <= maxWeight) return true;
        if (itemName == "medicine")
            if (weight + medicineW <= maxWeight) return true;

        return false;
    }

    /**
     * getters
     * 
     * @return amount of item left
     */
    public int getFood()
    {
        return food;
    }

    public int getWater()
    {
        return water;
    }

    public int getRepair()
    {
        return repair;
    }

    public int getMedicine()
    {
        return medicine;
    }

    public int getMoney()
    {
        return money;
    }

    public int getWeight()
    {
        return weight;
    }

    public int getMaxWeight()
    {
        return maxWeight;
    }
    //For save/load
    public int[] getQuantity(){
    	int[] qty = new int[6];
    	qty[0] = food;
    	qty[1] = water;
    	qty[2] = repair;
    	qty[3] = medicine;
    	qty[4] = money;
    	qty[5] = weight;
    	return qty;
    }
    //For save/load
    public void setQuantity(int[] items){
    	food = items[0];
    	water = items[1];
    	repair = items[2];
    	medicine = items[3];
    	money = items[4];
    	weight = items[5];
    }

    /**
     * Used to add stock to players items.
     * 
     * @param amount
     *            to add
     * @return new amount (int)
     */
    public int addFood(int amount)
    {
        food += amount;
        updateWeight();
        return food;
    }

    /**
     * Add amount of water
     * @param amount quantity
     * @return water current amount
     */
    public int addWater(int amount)
    {
        water += amount;
        updateWeight();
        return water;
    }

    /**
     * Repair items
     * @param amount quantity
     * @return repair current amount
     */
    public int addRepair(int amount)
    {
        repair += amount;
        updateWeight();
        return repair;
    }

    /**
     * Add amount of medicine
     * @param amount quantity
     * @return medicine current amount
     */
    public int addMedicine(int amount)
    {
        medicine += amount;
        updateWeight();
        return medicine;
    }

    /**
     * Add amount of money
     * @param amount quantity
     * @return money current amount
     */
    public int addMoney(int amount)
    {
        money += amount;
        updateWeight();
        return money;
    }

    /**
     * Used when the player consumes part of his item stock.
     * 
     * @param amount
     *            amount of item to remove
     * @return true if player had enough to consume amt, false otherwise
     */
    public boolean consumeFood(int amount)
    {
        if (food >= amount)
        {
            food -= amount;
            return true;
        }
        return false;
    }

    /**
     * Consume/Take away from stocked items
     * @param amount how much to take away
     * @return boolean if there is enough left
     */
    public boolean consumeWater(int amount)
    {
        if (water >= amount)
        {
            water -= amount;
            return true;
        }
        return false;
    }

    /**
     * Consume/Take away from stocked items
     * @param amount how much to take away
     * @return boolean if there is enough left
     */
    public boolean consumeRepair(int amount)
    {
        if (repair >= amount)
        {
            repair -= amount;
            return true;
        }
        return false;
    }

    /**
     * Consume/Take away from stocked items
     * @param amount how much to take away
     * @return boolean if there is enough left
     */
    public boolean consumeMoney(int amount)
    {
        if (money >= amount)
        {
            money -= amount;
            return true;
        }
        return false;
    }

    /**
     * Consume/Take away from stocked items
     * @param amount how much to take away
     * @return boolean if there is enough left
     */
    public boolean consumeMedicine(int amount)
    {
        if (medicine >= amount)
        {
            medicine -= amount;
            return true;
        }
        return false;
    }
}